<?php

/**
 * @category app
 * @package user
 * @subpackage uesr.model.user
 * @author nam nguyen
 * @version 1.0
 * @license LICENSE
 */
class User_Model_Table_Row_User extends Model_Table_Row
{
    /**
     * @var string
     */
    protected $_kind = 'user@user';

    protected $_role = null;

    /**
     * @return string
     */
    public function getRole()
    {
        if (!isset($this -> role))
        {
            $role = Nuo::getTable('core@role') -> find($this -> role_id) -> current();

            if (is_object($role))
            {
                $this -> role = $role -> getTitle();
            }
        }
        return $this -> role;
    }

    /**
     * get role id
     * @return int
     */
    public function getRoleId()
    {
        return isset($this -> role_id) ? $this -> role_id : 6;

    }

    /**
     * @return string
     */
    public function getTitle()
    {
        if (isset($this -> displayname) && $this -> displayname)
        {
            return $this -> displayname;
        }
        else
        if (isset($this -> username) && $this -> username)
        {
            return $this -> username;
        }
        else
        if (isset($this -> uid) && $this -> uid)
        {
            return $this -> uid;
        }

        return 'guest';
    }

    /**
     * get email
     * @return string
     */
    public function getEmail()
    {
        return isset($this -> email) ? $this -> email : '';
    }

    /**
     * @return string
     */
    public function getLastLoginDate()
    {
        return $this -> last_login_date;
    }

    /**
     * @return string
     */
    public function getGender()
    {
        return $this -> gender;
    }

    /**
     * @return string
     */
    public function getCountry()
    {
        return $this -> country;
    }

    /**
     * @return string
     */
    public function getLanguage()
    {
        return $this -> language;
    }

    /**
     * @return string
     */
    public function getTimezone()
    {
        return $this -> timezone;
    }

    /**
     * @return string
     */
    public function getLocale()
    {
        return $this -> locale;
    }

    /**
     * create password
     * @var string $password
     */
    public function setPassword($password)
    {
        /**
         * @var Plugin_Helper_String
         * @link /nuo/plugin/helper/string.php
         */
        $helper = Nuo::getHelper('@string');

        /**
         * reset salt string
         * @var string
         */
        $this -> salt = $helper -> createRandom(8, 1);

        /**
         * create hash password
         */
        $this -> password = $this -> createHash($password, $this -> salt);

        return $password;
    }

    /**
     * check $input is valid password of selected user
     * @param string $input
     */
    public function isValidPassword($input)
    {
        return (bool)($this -> createHash($input, $this -> salt) === $this -> password);
    }

    /**
     * @var string
     */
    static function createHash(/* call this method with multiple string*/)
    {
        $args = func_get_args();

        return sha1(implode('*', $args), 0);
    }

    public function toObject()
    {
        $result = parent::toArray();
        unset($result['salt']);
        unset($result['password']);
        return $result;
    }

}
